package com.hex.ds.hdrs.center.exec;

import com.hex.ds.hdrs.center.po.CenterConf;
import com.hex.ds.hdrs.center.service.CenterManager;
import com.hex.ds.hdrs.common.utils.HdrsDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Package: com.hex.ds.hdrs.center.exec
 * @ClassName CenterExec
 * @Description: 中心执行器
 * @Author: jd.chen
 * @Date 2021/7/9 16:34
 * @Version v1.0
 **/
@Slf4j
@EnableScheduling
@Component
public class CenterExec {

    @Autowired
    private CenterManager centerManager;

    @Value("${center.loseAgentLimit}")
    private int loseAgentLimit;

    /**
     * 【master】 和别的中心之间比较 有效节点数
     */
    //(fixedDelayString = "${scheduled.center.compareFixedDelay}", initialDelayString = "${scheduled.center.compareInitialDelay}")
    private void compareValidAgentNumExec() {
        log.info("master 和别的中心之间比较 有效节点数【CenterExec.compareValidAgentNumExec】开始=======》name: {}", Thread.currentThread().getName());
        if (centerManager.isMaster()) {
            int masterValidNum = centerManager.qryValidAgentNum();
            List<CenterConf> centerConfList = this.centerManager.getNoMasterCenter();

            for (CenterConf centerConf : centerConfList) {
                int validAgentNum = centerConf.getValidAgentNum();
                if (masterValidNum + loseAgentLimit < validAgentNum) {
                    log.warn("【警告】【触发切换】【CenterExec.compareValidAgentNumExec】");

                    return;
                }
            }
        }
    }

    /**
     * 所有中心的统计节点活跃数
     */
    //@Scheduled(fixedDelayString = "${scheduled.center.agentRunFixedDelay}", initialDelayString = "${scheduled.center.agentInitialDelay}")
    private void checkValidAgentNumExec() {
        log.info("所有中心的统计节点活跃数【CenterExec.checkValidAgentNumExec】开始=======》name: {}", Thread.currentThread().getName());
        int validAgentNum = 0;
        if (centerManager.isMaster()) {
            log.info("【1】【master】【CenterExec.checkValidAgentNumExec】");
            validAgentNum = centerManager.getMasterValidAgentNum();

            if (validAgentNum < loseAgentLimit) {
                log.warn("【警告】【master】【触发切换】【CenterExec.checkValidAgentNumExec】");

            }
        } else {
            log.info("【1】【非master】【CenterExec.checkValidAgentNumExec】");
            validAgentNum = centerManager.getValidAgentNum();
        }

        log.info("【2】更新有效节点数【CenterExec.checkValidAgentNumExec】");
        this.centerManager.upValidAgentNum(validAgentNum, centerManager.getCenterCode());
    }

    /**
     * 内存队列数量统计
     */
    @Scheduled(fixedDelayString = "${scheduled.ramQueue.quantityStatisticsFixedDelay}", initialDelayString = "${scheduled.ramQueue.quantityStatisticsInitialDelay}")
    public void ramQueueQuantityStatistics() {
        log.info("内存数量统计【CenterExec.ramQueueQuantityStatistics】-【{}】开始时间: {}", Thread.currentThread().getName(), HdrsDateUtil.getCurrentTime());
        try {
            if (centerManager.isMaster()) {
                this.centerManager.ramQueueQuantityStatistics();
            }
        } catch (Exception e) {
            log.error("【ramQueueQuantityStatistics】异常：{}", e.getMessage(), e);
        } finally {
            log.info("内存数量统计【CenterExec.ramQueueQuantityStatistics】-【{}】结束时间: {}", Thread.currentThread().getName(), HdrsDateUtil.getCurrentTime());
        }
    }

}
